<?php

namespace app\controller;
use think\facade\Db;
use think\Request;

class Xhsdataurl extends Common
{
	public function read(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhsdataurl:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$order = $request->param('order', 'desc', 'trim');
		$sort = $request->param('sort', 'lasttime desc, id desc', 'trim');

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if (empty($bazaarId) == false) {
			array_push($where, ['bazaarId', '=', $bazaarId]);
		}

		$userStat = $request->param('userStat', '', 'trim');
		if (empty($userStat) == false) {
			array_push($where, ['type', '=', $userStat]);
		}

		$company = $request->param('company', '', 'trim');
		if (empty($company) == false) {
			array_push($where, ['company', 'like', "%{$company}%"]);
		}

		$keywords = $request->param('keywords', '', 'trim');
		if (empty($keywords) == false) {
			array_push($where, ['keywords', 'like', "%{$keywords}%"]);
		}

		$title = $request->param('title', '', 'trim');
		if (empty($title) == false) {
			array_push($where, ['title', 'like', "%{$title}%"]);
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			$time = cloud_datetime_format( $createTimeStart, $createTimeEnd, true);
		} else {
			$time = cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true);
		}
		array_push($where, ['lasttime', 'BETWEEN', $time]);

		$paginate = Db::name('cfg_xhs_url')->field(
				'`id`, `bazaarId`, `type`, `title`, `company`, `locationTV`, `comments`, `datetime`, `showpos`, `love`, `star`, `url`, `urlex`, `keywords`, `number`, FROM_UNIXTIME(`time`, \'%Y-%m-%d %H:%i:%s\') as `time`, FROM_UNIXTIME(lasttime, \'%Y-%m-%d %H:%i:%s\') as lasttime'
		)->where($where)->order($sort, $order)->paginate($limit);

		$xhsBazaar = Db::table('cfg_xhs_bazaar')->field('id, name')->where('status', 1)->select()->toArray();

		return json(['code' => 0, 'msg' => '', 'count' => $paginate->total(), 'data' => $paginate->items(), 'bazaar' => $xhsBazaar]);
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function dels(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhskeywords:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$index = 0;
		$temp = [];
		$deltitle = $request->param('deltitle', '', 'trim');
		$array = explode("\n", $deltitle);
		foreach ($array as $value) {
			if($value){
				array_push($temp, $value);
			}
		}
		if(count($temp)==0) {
			return json(['code' => 0, 'msg' => '成功删除' . $index . '个笔记']);
		}

		$where = [];
		$bazaarId = $request->param('bazaarId', '', 'trim');
		if (empty($bazaarId) == false) {
			array_push($where, ['bazaarId', '=', $bazaarId]);
		}

		$userStat = $request->param('userStat', '', 'trim');
		if (empty($userStat) == false) {
			array_push($where, ['type', '=', $userStat]);
		}

		$company = $request->param('company', '', 'trim');
		if (empty($company) == false) {
			array_push($where, ['company', 'like', "%{$company}%"]);
		}

		$keywords = $request->param('keywords', '', 'trim');
		if (empty($keywords) == false) {
			array_push($where, ['keywords', 'like', "%{$keywords}%"]);
		}

		$title = $request->param('title', '', 'trim');
		if (empty($title) == false) {
			array_push($where, ['title', 'like', "%{$title}%"]);
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			$time = cloud_datetime_format( $createTimeStart, $createTimeEnd, true);
		} else {
			$time = cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true);
		}
		array_push($where, ['lasttime', 'BETWEEN', $time]);

		$query = Db::name('cfg_xhs_url')->field('`id`, `title`')->where($where)->select()->toArray();
		foreach ($query as $value) {
			if(empty($value['title'])) {
				Db::table('cfg_xhs_url')->where('id', $value['id'])->delete();
			}
		}

		$query = Db::name('cfg_xhs_url')->field('`id`, `title`')->where($where)->select()->toArray();
		foreach ($query as $value) {
			$result = preg_match_all('('.implode('|', $temp).')', $value['title']);
			if( $result ) {
				$index++;
				Db::table('cfg_xhs_url')->where('id', $value['id'])->delete();
			}
		}
		return json(['code' => 0, 'msg' => '成功删除' . $index . '个笔记']);
	}

}
